package httpclient;

import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.InputStream;


/**
 * Created by Administrator on 2017/5/25.
 */
public class AccessHeander {
    public static void main(String[] args) throws IOException {
        CloseableHttpClient httpClient= HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://httpbin.org/get");
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            System.out.println(response.getStatusLine());
            HttpEntity entity1 = response.getEntity();
            // do something useful with the response body
            // and ensure it is fully consumed
            EntityUtils.consume(entity1);
            System.out.println(entity1.getContentType().toString());
            System.out.println(entity1.getContentEncoding().toString());
            System.out.println();
            InputStream inputStream =entity1.getContent();
            byte[] bytes = new byte[2048];
            int reader;
            while((reader=inputStream.read())!=-1){
                inputStream.read(bytes);
            }
            System.out.println(bytes.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            response.close();
        }
    }
}
